package com.example.anpnovel.controller.crud;

import com.example.anpapi.entity.po.novelmodel.ChapterVersion.ChapterVersion;
import com.example.anpapi.entity.vo.R;
import com.example.anpnovel.service.interfaces.ChapterVersionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/chapter-version")
public class ChapterVersionController {

    @Autowired
    private ChapterVersionService chapterVersionService;

    /**
     * 章节版本添加
     * @param chapterVersion
     * @return
     */
    @PostMapping("/save")
    public R<ChapterVersion> save(@RequestBody ChapterVersion chapterVersion){
        log.info("章节版本添加:{}", chapterVersion);
        chapterVersionService.save(chapterVersion);
        return R.success(chapterVersion,"章节版本添加成功");
    }

    /**
     * 根据版本id删除章节版本
     * @param chapterVersion
     * @return
     */
    @DeleteMapping("/remove")
    public R<String> remove(@RequestBody ChapterVersion chapterVersion){
        log.info("章节版本删除:{}", chapterVersion);
        if(chapterVersionService.getById(chapterVersion.getId()) != null)
        {
            chapterVersionService.removeById(chapterVersion.getId());
            return R.success(null,"章节版本删除成功");
        }
        return R.error("未找到该章节版本");
    }

    /**
     * 根据版本id查找章节版本
     * @param id
     * @return
     */
    @GetMapping("/get")
    public R<ChapterVersion> get(@RequestParam(value = "id") Long id) {
        log.info("查询章节版本的id为{}", id);
        ChapterVersion chapterVersion = chapterVersionService.getById(id);
        return R.success(chapterVersion, "查询章节版本成功");
    }

    /**
     * 根据章节id查找所有版本
     * @param chapterId
     * @return
     */
    @GetMapping("/getByChapterId")
    public R<List<ChapterVersion>> getByChapterId(@RequestParam(value = "chapterId") Long chapterId) {
        log.info("查询章节id为{}的所有版本", chapterId);
        List<ChapterVersion> chapterVersions = chapterVersionService.lambdaQuery()
                .eq(ChapterVersion::getChapterId, chapterId)
                .orderByDesc(ChapterVersion::getVersionNumber)
                .list();
        return R.success(chapterVersions, "查询章节版本成功");
    }

    /**
     * 修改章节版本
     * @param chapterVersion
     * @return
     */
    @PostMapping("/update")
    public R<ChapterVersion> update(@RequestBody ChapterVersion chapterVersion){
        log.info("修改的章节版本为{}", chapterVersion);
        chapterVersionService.updateById(chapterVersion);
        return R.success(chapterVersion,"修改章节版本成功");
    }

}

